package com.doujiang.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.doujiang.domain.Movie;
import com.doujiang.domain.Rating;
import com.doujiang.domain.dto.HotMovieDTO;
import com.doujiang.domain.dto.MovieDTO;
import com.doujiang.domain.dto.ReviewStatsDTO;
import com.doujiang.service.IMovieService;
import com.doujiang.util.PageDomain;
import com.doujiang.util.TableDataInfo;
import com.doujiang.util.TableSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/movies")
@CrossOrigin
public class MovieController {

    @Autowired
    private IMovieService movieService;

    /**
     * 获取所有电影
     */
    @GetMapping
    public ResponseEntity<?> getAllMovies() {
        List<Movie> movies = movieService.list();
        return ResponseEntity.ok(movies);
    }

    /**
     * 根据ID获取电影
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> getMovieById(@PathVariable("id") Long id) {
        Movie movie = movieService.getById(id);
        if (movie != null) {
            return ResponseEntity.ok(movie);
        } else {
            return ResponseEntity.status(404).body("电影不存在");
        }
    }

    /**
     * 批量根据ID获取电影
     */
    @GetMapping(params = "ids")
    public ResponseEntity<List<Movie>> getMoviesByIds(@RequestParam("ids") String ids) {
        try {
            String[] idArr = ids.split(",");
            List<Long> idList = new ArrayList<>();
            for (String idStr : idArr) {
                try {
                    idList.add(Long.parseLong(idStr.trim()));
                } catch (Exception ignore) {}
            }
            if (idList.isEmpty()) return ResponseEntity.ok(new ArrayList<>());
            List<Movie> movies = movieService.listByIds(idList);
            return ResponseEntity.ok(movies);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(null);
        }
    }

    /**
     * 获取热门电影
     */
    @GetMapping("/hot")
    public ResponseEntity<?> getHotMovies(@RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        List<HotMovieDTO> hotMovies = movieService.getHotMovies(limit);
        return ResponseEntity.ok(hotMovies);
    }

    /**
     * 获取影评统计数据
     */
    @GetMapping("/stats")
    public ResponseEntity<?> getReviewStats() {
        ReviewStatsDTO stats = movieService.getReviewStats();
        return ResponseEntity.ok(stats);
    }

    /**
     * 查询电影
     * 刘跃
     *
     * @return 评分管理
     */
    @GetMapping("/selectMovieList")
    public TableDataInfo selectMovieList(MovieDTO movieDTO)
    {
        // 获取分页参数
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();

        // 获取全部数据
        List<MovieDTO> allList = movieService.selectMovieList(movieDTO);

        // 计算分页
        int total = allList.size();
        int fromIndex = (pageNum - 1) * pageSize;
        int toIndex = Math.min(fromIndex + pageSize, total);

        // 手动分页
        List<MovieDTO> pageList = new ArrayList<>();
        if (fromIndex < total) {
            pageList = allList.subList(fromIndex, toIndex);
        }

        TableDataInfo dataTable = new TableDataInfo();
        dataTable.setRows(pageList);
        dataTable.setCode(200);
        dataTable.setTotal(total);
        return dataTable;
    }

    /**
     * 通过搜索框查询电影
     * 刘跃
     */
    @GetMapping("/searchMovie")
    public TableDataInfo searchMovie(MovieDTO movieDTO)
    {
        List<MovieDTO> list = movieService.searchMovieList(movieDTO);

        TableDataInfo dataTable = new TableDataInfo();
        dataTable.setRows(list);
        dataTable.setCode(200);
        return dataTable;
    }
} 